<template>
  <el-dialog
    title="添加广告配置"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :modal="false"
    :before-close="handleCloseDialog"
    class="configs-member-dialog"
    width="680px"
  >
    <el-form
      :model="dataObject"
      :rules="rules"
      ref="adminForm"
      label-width="100px"
      size="small"
    >
      <el-form-item label="会员ID：" prop="memberId">
        <el-input v-model="dataObject.memberId" disabled></el-input>
      </el-form-item>
      <el-form-item label="广告：">
        <div class="goods-list" @click="handleAddGoods">
          <div v-if="goodsList.length === 0" class="placeholder">
            请选择广告
          </div>
          <template v-else>
            <el-tag v-for="tag in goodsList" :key="tag.id">
              {{ tag.goodsName }}
            </el-tag>
          </template>
        </div>
      </el-form-item>
      <el-form-item label="排序：" prop="sort">
        <el-input
          v-model="dataObject.sort"
          clearable
          placeholder="请输入排序"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="handleConfirmDialog('adminForm')"
        size="mini"
        >确 定</el-button
      >
      <el-button @click="handleCloseDialog" size="mini">取 消</el-button>
    </span>
    <!-- 添加广告配置 -->
    <goods-list-dialog
      v-if="goodsVisible"
      :goodsList="goodsList"
      @handle-confirm="handleConfirm"
      @handle-cancel="goodsVisible = false"
    />
  </el-dialog>
</template>
<script>
import { updateBankInfo } from "@/api/member";
import { createNamespacedHelpers } from "vuex";
import goodsListDialog from "@/views/goods/list/list.vue";
const { mapState } = createNamespacedHelpers("member");

const defaultObject = {
  memberId: "",
  goodsId: [],
  sort: ""
};

export default {
  components: {
    goodsListDialog
  },
  data() {
    return {
      visible: true,
      loading: false,
      goodsVisible: false,
      dataObject: Object.assign({}, defaultObject),
      goodsList: []
    };
  },
  props: {
    row: {
      type: Object,
      default: {}
    }
  },
  computed: {
    ...mapState({
      rules: state => state.rules,
      networkStatusOptions: state => state.routes.networkStatus
    })
  },
  mounted() {
    this.dataObject.memberId = this.row.id;
  },
  methods: {
    handleCloseDialog() {
      this.$emit("handle-cancel");
    },
    handleConfirmDialog(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          updateBankInfo({ ...this.dataObject, memberId: this.row.id }).then(
            response => {
              this.resetForm(formName);
              this.$message({
                message: "提交成功",
                type: "success",
                duration: 1000
              });
              this.$emit("handle-confirm");
            }
          );
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dataObject = Object.assign({}, defaultObject);
    },
    //确定选择广告
    handleConfirm(val) {
      this.goodsList = val;
      this.goodsVisible = false;
      updateBankInfo({ ...this.dataObject, memberId: this.row.id }).then(
            response => {
              this.resetForm(formName);
              this.$message({
                message: "提交成功",
                type: "success",
                duration: 1000
              });
              this.$emit("handle-confirm");
            }
          );
    },
    //添加广告
    handleAddGoods() {
      this.goodsVisible = true;
    }
  }
};
</script>
<style scoped lang="scss">
.goods-list {
  cursor: pointer;
  border: solid 1px #ddd;
  border-radius: 4px;
  padding: 10px 15px;

  .placeholder {
    height: 100%;
    display: flex;
    align-items: center;

    color: #cdcdcd;
  }
}
</style>

<style lang="scss">
.configs-member-dialog {
  .el-dialog__body {
    height: 30vh !important;
  }
}
</style>
